package clueless.server;

import java.util.ArrayList;

import clueless.utilities.Card;
import clueless.utilities.Character;
import clueless.utilities.Map;
import clueless.utilities.Room;
import clueless.utilities.Suggestion;
import clueless.utilities.Weapon;

public class Game {
	
	Card[] solution;
	
	Map m_gameMap;
	int m_currentPlayer;

	
	public Map getGameMap()
	{
		return m_gameMap;
	}
	
	ArrayList<Player> m_players;
	
	public int getNumPlayers() {
		return m_players.size();
	}

	public void addPlayer(Player player){
		m_players.add(player);
		player.setStartingRoom(m_gameMap);
	}

	public Game() {
		super();
		m_players = new ArrayList<Player>();
		m_gameMap = new Map();
		solution = new Card[3];
	}

	public boolean enoughPlayers() {
		if (m_players.size() > 2)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	private ArrayList<Card> makeCharacterCards() {
		ArrayList<Card> cards = new ArrayList<Card>();
		cards.add(new Card("character", Character.m_COL_MUSTARD));
		cards.add(new Card("character", Character.m_MISS_SCARLET));
		cards.add(new Card("character", Character.m_MR_GREEN));
		cards.add(new Card("character", Character.m_MRS_PEACOCK));
		cards.add(new Card("character", Character.m_MRS_WHITE));
		cards.add(new Card("character", Character.m_PROF_PLUM));
		return cards;
	}
	
	private ArrayList<Card> makeWeaponCards() {
		ArrayList<Card> cards = new ArrayList<Card>();
		cards.add(new Card("weapon", Weapon.m_CANDLESTICK));
		cards.add(new Card("weapon", Weapon.m_KNIFE));
		cards.add(new Card("weapon", Weapon.m_LEAD_PIPE));
		cards.add(new Card("weapon", Weapon.m_REVOLVER));
		cards.add(new Card("weapon", Weapon.m_ROPE));
		cards.add(new Card("weapon", Weapon.m_WRENCH));
		return cards;
	}
	
	private ArrayList<Card> makeRoomCards() {
		ArrayList<Card> cards = new ArrayList<Card>();
		cards.add(new Card("room", Room.m_BALLROOM));
		cards.add(new Card("room", Room.m_BILLIARD_ROOM));
		cards.add(new Card("room", Room.m_CONSERVATORY));
		cards.add(new Card("room", Room.m_DINING_ROOM));
		cards.add(new Card("room", Room.m_HALL));
		cards.add(new Card("room", Room.m_KITCHEN));
		cards.add(new Card("room", Room.m_LIBRARY));
		cards.add(new Card("room", Room.m_LOUNGE));
		cards.add(new Card("room", Room.m_STUDY));
		return cards;
	}

	public void distributeCards()
	{
		ArrayList<Card> characters = makeCharacterCards();
		ArrayList<Card> weapons = makeWeaponCards();
		ArrayList<Card> rooms = makeRoomCards();
		
		int characterIndex = (int)(Math.random()*Character.m_numCharacters);
		solution[0] = characters.remove(characterIndex);
		
		int weaponIndex = (int)(Math.random()*Weapon.m_numWeapons);
		solution[1] = weapons.remove(weaponIndex);
		
		int roomIndex = (int)(Math.random()*Room.m_numRooms);
		solution[2] = rooms.remove(roomIndex);
		
		ArrayList<Card> allCards = characters;
		allCards.addAll(weapons);
		allCards.addAll(rooms);
		
		while (!allCards.isEmpty()) {
			for (Player player : m_players) {
				if (!allCards.isEmpty()) {
					int index = (int)(Math.random()*allCards.size());
					player.addCard(allCards.remove(index));
				}
			}
		}
		
	}
	
	public Suggestion getSolution() {
		Suggestion ret = new Suggestion(solution[0].getValue(),solution[1].getValue(), solution[2].getValue());
		return ret;
	}

	public Player getCurrentPlayer() {
		return m_players.get(m_currentPlayer);
	}

	public Player getPlayer(int i) {
		return m_players.get((m_currentPlayer+i)%m_players.size() );
	}

	public void endTurn() {
		
		m_currentPlayer = (m_currentPlayer+1)%m_players.size();
		if (!getCurrentPlayer().getIsStillPlaying()) {
			endTurn();
		}
	}

	public boolean verifyAccusation(Suggestion suggestion) {
		if ( solution[0].getValue().equalsIgnoreCase(suggestion.getCharacter()) &&
			 solution[1].getValue().equalsIgnoreCase(suggestion.getWeapon()) &&
			 solution[2].getValue().equalsIgnoreCase(suggestion.getRoom())) {
			return true;
		}
		
		m_players.get(m_currentPlayer).setIsStillPlaying(false);
		return false;
	}

	public Player getNextActivePlayer() {
		for (int i = 1; i < m_players.size(); i++) {
			if (getPlayer(i).getIsStillPlaying())
			{
				return  getPlayer(i);
			}
		}
		return null;
	}

	public void MovePlayerToRoom(String character, String room) {
		
		for (Player player : m_players) {
			if (player.getCharacter().equalsIgnoreCase(character)) {
				player.setRoom(room, m_gameMap);				
			}
		}
	}
	
}
